package com.yzkj.cpjxc.business.framework.utils;

import com.yzkj.cpjxc.business.framework.common.Constants;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 字符串帮助类
 */
public class StringHelp {

	/**
	 * 首字母大写
	 * 
	 * @param s
	 * @return
	 */
	public static String firstLetterToUpperCase(String s) {
		return s.substring(0, 1).toUpperCase() + s.substring(1);
	}

	/**
	 * 首字母小写
	 * 
	 * @param s
	 * @return
	 */
	public static String firstLetterToLowerCase(String s) {
		return s.substring(0, 1).toLowerCase() + s.substring(1);
	}

	/**
	 * 分解单词直接首字母是大写的单词：如DimWebServiceImpl ===>  {Dim, Web, Service, Impl}
	 * @modifier:
	 * @modifiedDate:
	 * @param word
	 * @return
	 */
	public static String[] resolveWord(String word) {
		boolean hasUpperCase = false;
		// 1、把大写字母的位置找出来，并放到一个List<Integer>集合里去
		char[] cs = word.toCharArray();
		List<Integer> indexList = new ArrayList<Integer>();
		for (int i = 0; i < cs.length; i++) {
			if (isUpperCase(cs[i])) {
				indexList.add(i);
				hasUpperCase = true;
			}
		}

		if (!hasUpperCase) {
			return new String[] {
				word
			};
		}

		// 2、根据List<Integer>集合里面存放的下标对这个单词进行截取
		List<String> resultList = new ArrayList<String>();
		for (int i = 0; i < indexList.size(); i++) {
			// 2.1 如果第一个大写字母从0开始
			if (indexList.get(0) == 0) {
				if (i == indexList.size() - 1) {
					//strs[i] = word.substring(indexList.get(i));
					resultList.add(word.substring(indexList.get(i)));
				} else {
					//strs[i] = word.substring(indexList.get(i), indexList.get(i+1));
					resultList.add(word.substring(indexList.get(i), indexList.get(i + 1)));
				}
			} else {
				// 2.2 如果第一个大写字母不从0开始
				if (indexList.size() == 1) { // 2.2.1 如果只有一个大写字母

					resultList.add(word.substring(0, indexList.get(i)));
					resultList.add(word.substring(indexList.get(i)));

				} else { // 2.2.2 如果大写字母大于1个

					if (i == indexList.size() - 1) {// 如果是最后一个大写字母,那么从这个下标开始截取
						resultList.add(word.substring(indexList.get(i)));
					} else {
						if (i == 0) {// 先从下标0到第一个大写字母的下标截取
							resultList.add(word.substring(0, indexList.get(i)));
						}
						// 截取当前大写字母的下标到下一个大写字母的下标
						resultList.add(word.substring(indexList.get(i), indexList.get(i + 1)));
					}
				}
			}
		}
		String[] strs = new String[resultList.size()];
		return resultList.toArray(strs);
	}

	/**
	 * 判断该字母是否是大写
	 * @modifier:
	 * @modifiedDate:
	 * @param c
	 */
	public static boolean isUpperCase(char c) {
		return (byte) c >= 65 && (byte) c <= 90;
	}

	/**
	 * 判断该字母是否是小写
	 * @modifier:
	 * @modifiedDate:
	 * @param c
	 */
	public static boolean isLowerCase(char c) {
		return (byte) c >= 97 && (byte) c <= 122;
	}
	
	/**
	 * 将十六进制字符串转化为byte
	 * @modifier:
	 * @modifiedDate:
	 * @param hexString
	 * @return
	 */
	public static byte[] hexStringToBytes(String hexString) {
		
		if (StringUtils.isEmpty(hexString)){
			return null;
		}
		
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] hexByte = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			hexByte[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return hexByte;
		
	}
	
	/**
	 * char转为byte
	 * @modifier:
	 * @modifiedDate:
	 * @param c
	 * @return
	 */
	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}
	public static void main(String[] args) {
		System.out.println(firstLetterToLowerCase("ADDDD_DTTTTTTTT"));
	}
	
	
	/**
	 * @description
	 * @modifier:
	 * @modifiedDate:
	 * @param clazz
	 * @param obj
	 */
	public static void replaceFilteringChars(Class clazz, Object obj){
		List<String> chars = Constants.getFilteringChars();
		
		if(clazz!=null && obj!=null){
			Field[] fields = clazz.getDeclaredFields();
			Field f = null;
			String value = "";
			for (int i = 0; i < fields.length; i++) {
				f = fields[i];
				if ("java.lang.String".equalsIgnoreCase(f.getType().getName())) {
					
					f.setAccessible(true);
					try {
						if(f.get(obj)!=null){
							value = f.get(obj).toString();
							for(String c : chars){
								value = value.replace(c, "");
							}
							f.set(obj,value);
						}
					} catch (IllegalArgumentException | IllegalAccessException e) {
						e.printStackTrace();
					}
				}
			}
			
		}
		
	}
}
